package ar.edu.unicen.pladema.ui.model.g3d;

public class Matrix {
	private static final int SIZE = 4;
	private static final int COL_X = 0;
	private static final int COL_Y = 1;
	private static final int COL_Z = 2;
	private static final int COL_W = 3;
	protected double[][] matrix;
	public Matrix() {
		matrix = new double[SIZE][SIZE];
		for(int i=0; i<SIZE;i++) {
			for(int j=0;j<SIZE;j++) {
				matrix[i][j] = 0;
			}
		}
	}
	public void setValue(int col, int row, double v) {
		matrix[col][row]=v;
	}
	public void setEscala(double scale) {
		matrix[0][0] = scale;
		matrix[1][1] = scale;
		matrix[2][2] = scale;
	}
	public Vertex multiply(Vertex vertex) {
		Vertex result = new Vertex();
		result.setDX(mrow(vertex, COL_X));
		result.setDY(mrow(vertex, COL_Y));
		result.setDZ(mrow(vertex, COL_Z));
		return result;
	}
	private double mrow(Vertex v, int col) {
		return (v.getDX() * matrix[col][COL_X]) + 
				(v.getDY() * matrix[col][COL_Y]) + 
				(v.getDZ() * matrix[col][COL_Z]) + 
				(matrix[col][COL_W]);
	}
	public Matrix multiply(Matrix other) {
		Matrix result = new Matrix();
		for (int col=0; col<SIZE; col++) {
			for (int row=0; row<SIZE; row++) {
				result.setValue(col, row, mcr(this, other, col, row));
			}
		}
		return result;
	}
	private static double mcr(Matrix a, Matrix b, int col, int row) {
		double result = a.matrix[col][0]*b.matrix[0][row];
		result += a.matrix[col][1]*b.matrix[1][row];
		result += a.matrix[col][2]*b.matrix[2][row];
		result += a.matrix[col][3]*b.matrix[3][row];
		return result;
	}

}
